// subpkg/game/game.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:'今日饮食',
    show: false,
    currentDate: new Date().getTime(),
    minDate: new Date(2023, 8, 1).getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    foodList:
    [
      {
        image_src:'/static/早餐.png',
        text:'早餐'
      },
      {
        image_src:'/static/午餐.png',
        text:'午餐'
      },
      {
        image_src:'/static/晚餐.png',
        text:'晚餐'
      },
      {
        image_src:'/static/加餐.png',
        text:'加餐'
      }
    ],
    detailList:
    [
      {
        text:'早餐',
        suggestion:'建议332~663千卡',
        eatCount:'350千卡',
        list:
        [
          {
            image_src:'/static/晚餐.png',
            count:'1'
          }
        ]
      },
      {
        text:'午餐',
        suggestion:'建议1000~1500千卡',
        eatCount:'0千卡',
        list:
        [
         
        ]
      },
      {
        text:'晚餐',
        suggestion:'建议500~800千卡',
        eatCount:'0千卡',
        list:
        [
          
        ]
      },
    ],
    breakfast:
    [],

    lunch:
    [],
    
    dinner:
    []

    
  },

  onClose() {
    this.setData({ show: false });
  },
  onConfirm(){
    this.setData({
      date:this.data.date1,
      show:false
    })
    this.queryBreakfast(this.data.date)
    this.queryLunch(this.data.date)
    this.queryDinner(this.data.date)

  },
  onCancel(){
    this.setData({
      show:false
    })
  },
  onInput(event) {
    var date = new Date(event.detail)
    var year = date.getFullYear()
    var month = date.getMonth()+1 <10 ? `0${date.getMonth()+1}` : date.getMonth()+1
    var day = date.getDate()<10?`0${date.getDate()}` : date.getDate()
    var currentDate = `${year}-${month}-${day}`
    console.log(currentDate)
    this.setData({
      date1: currentDate,
    });
  },
  show(){
    this.setData({
      show:true
    })
  },
  showPopup() {
    this.setData({ show: true });
  },

   //早餐预览图片
   imgClick(e){
    var imgUrl = this.data.breakfast[this.data.breakfast.length-1].picture
    wx.previewImage({
      urls: imgUrl,
      success:(res=>{
        console.log(res)
      })
    })
  },

  //午餐预览图片
  imgClick1(e){
   var imgUrl = this.data.lunch[this.data.lunch.length-1].picture
   var url = []
   url.push(imgUrl)
   console.log(imgUrl)
   wx.previewImage({
     urls: url,
     success:(res=>{
       console.log(res)
     })
   })
 },

 //晚餐预览图片
 imgClick2(e){
 var imgUrl = this.data.dinner[this.data.dinner.length-1].picture
 console.log(imgUrl)
 wx.previewImage({
   urls: imgUrl,
   success:(res=>{
     console.log(res)
   })
 })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth()+1 <10 ? `0${date.getMonth()+1}` : date.getMonth()+1
    var day = date.getDate()<10?`0${date.getDate()}` : date.getDate()
    var currentDate = `${year}-${month}-${day}`
    
    this.queryBreakfast(currentDate)
    this.queryLunch(currentDate)
    this.queryDinner(currentDate)
  },
  queryLunch(date){
    const that = this
    wx.request({
      url: 'https://zhixiao.site:8080/client/lunch/query',
      data:{
        date:date,
        elderId:wx.getStorageSync('elderId')
      },
      header:{
        'Cookie':wx.getStorageSync('JSESSIONID')
      },
      success:(res)=>{
        console.log("午餐")
        console.log(res)
        if(res.data.data.length===0){
          this.setData({
            lunch:[]
          })
          return
        }
        var lunch = []
        var lunch1 = res.data.data[0]
        console.log(res.data.data[0])
        for(var i in lunch1.assistedFood){
          if(lunch1.assistedFood[i].count!=0){
            var obj = {}
            obj.name = lunch1.assistedFood[i].name
            obj.count = lunch1.assistedFood[i].count
            obj.unit = '个'
            lunch.push(obj)
          }
        }
        for(var i in lunch1.stapleFood){
          if(lunch1.stapleFood[i].count!=0){
            var obj = {}
            obj.name = lunch1.stapleFood[i].name
            obj.count = lunch1.stapleFood[i].count
            obj.unit = '碗'
            lunch.push(obj)
          }
        }
        for(var i in lunch1.meatDishs){
          if(lunch1.meatDishs[i].count!=0){
            var obj = {}
            obj.name = lunch1.meatDishs[i].name
            obj.count = lunch1.meatDishs[i].count
            obj.unit = '块'
            lunch.push(obj)
          }
        }
        for(var i in lunch1.vegetableDishs){
          if(lunch1.vegetableDishs[i].count!=0){
            var obj = {}
            obj.name = lunch1.vegetableDishs[i].name
            obj.count = lunch1.vegetableDishs[i].count
            obj.unit = '块'
            lunch.push(obj)
          }
        }
        for(var i in lunch1.soup){
          if(lunch1.soup[i].count!=0){
            var obj = {}
            obj.name = lunch1.soup[i].name
            obj.count = lunch1.soup[i].count
            obj.unit = '碗'
            lunch.push(obj)
          }
        }
        for(var i in lunch1.others){
          if(lunch1.others[i].count!=0){
            lunch.push(lunch1.others[i])
          }
        }
        this.setData({
          lunch:lunch
        })
      }
    })
  },

  queryDinner(date){
    wx.request({
      url: 'https://zhixiao.site:8080/client/dinner/query',
      data:{
        date:date,
        elderId:wx.getStorageSync('elderId'),
      },
      header:{
        'Cookie':wx.getStorageSync('JSESSIONID')
      },
      success:(res)=>{
        console.log("晚餐")
        console.log(res)
        if(res.data.data.length===0){
          this.setData({
            dinner:[]
          })
          return
        }
        var dinner = []
        var dinner1 = res.data.data[0]
        console.log(res.data.data[0])
        for(var i in dinner1.assistedFood){
          if(dinner1.assistedFood[i].count!=0){
            var obj = {}
            obj.name = dinner1.assistedFood[i].name
            obj.count = dinner1.assistedFood[i].count
            obj.unit = '个'
            dinner.push(obj)
          }
        }
        for(var i in dinner1.stapleFood){
          if(dinner1.stapleFood[i].count!=0){
            var obj = {}
            obj.name = dinner1.stapleFood[i].name
            obj.count = dinner1.stapleFood[i].count
            obj.unit = '碗'
            dinner.push(obj)
          }
        }
        for(var i in dinner1.meatDishs){
          if(dinner1.meatDishs[i].count!=0){
            var obj = {}
            obj.name = dinner1.meatDishs[i].name
            obj.count = dinner1.meatDishs[i].count
            obj.unit = '块'
            dinner.push(obj)
          }
        }
        for(var i in dinner1.vegetableDishs){
          if(dinner1.vegetableDishs[i].count!=0){
            var obj = {}
            obj.name = dinner1.vegetableDishs[i].name
            obj.count = dinner1.vegetableDishs[i].count
            obj.unit = '块'
            dinner.push(obj)
          }
        }
        for(var i in dinner1.soup){
          if(dinner1.soup[i].count!=0){
            var obj = {}
            obj.name = dinner1.soup[i].name
            obj.count = dinner1.soup[i].count
            obj.unit = '碗'
            dinner.push(obj)
          }
        }
        for(var i in dinner1.others){
          if(dinner1.others[i].count!=0){
            dinner.push(dinner1.others[i])
          }
        }
        this.setData({
          dinner:dinner
        })
      }

    })
  },
  queryBreakfast(date){
    wx.request({
      url: 'https://zhixiao.site:8080/client/breakfast_query',
      data:{
        date:date,
        elderId:wx.getStorageSync('elderId'),
      },
      header:{
        'Cookie':wx.getStorageSync('JSESSIONID')
      },
      success:(res)=>{
        console.log("早餐")
        console.log(res)
        if(res.data.data==null){
          this.setData({
            breakfast:[]
          })
          return
        }
        const breakfast = []
        var breakfast1 = res.data.data
        for(var i in breakfast1.drinkList){
          if(breakfast1.drinkList[i].count!=0){
            var obj = {}
            obj.name = breakfast1.drinkList[i].name
            obj.count = breakfast1.drinkList[i].count
            obj.unit = '碗'
            breakfast.push(obj)
          }
        }
        for(var i in breakfast1.foodList){
          if(breakfast1.foodList[i].count!=0){
            var obj = {}
            obj.count = breakfast1.foodList[i].count
            obj.name = breakfast1.foodList[i].name
            if(breakfast1.foodList[i].name =='面条'||breakfast1.foodList[i].name == '米饭')
            {
              obj.unit = '碗'
            }else{
              obj.unit = '个'
            }
            
            
            breakfast.push(obj)
          }
        }
        for(var i in breakfast1.eggMilkList){
          if(breakfast1.eggMilkList[i].count!=0){
            var obj = {}
            obj.name = breakfast1.eggMilkList[i].name
            obj.count = breakfast1.eggMilkList[i].count
            if(breakfast1.eggMilkList[i].name=='鸡蛋'){
              obj.unit = '个'
            }else{
              obj.unit = '杯'
            }
          }
        }
        for(var i in breakfast1.others){
          if(breakfast1.others[i].count!=0){
            breakfast.push(breakfast1.others[i])
          }
        }
        this.setData({
          breakfast:breakfast
        })
      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})